package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import models.User;
import utils.DBconn;

public class UserDaoImpl implements UserDao {

    /*
     *检查:检查用户名是否存在。密码不做检查
     */
    public boolean checkUserInfo(User user) throws SQLException {
        boolean flag = false;
        DBconn.init();
        //查询是否存在该用户名
        ResultSet rs = DBconn.selectSql("select * from voicerobot.user_info where user_name='" + user.getName() + "';");
        while (rs.next()) {
            if (rs.getString("user_name").equals(user.getName())) {
                //存在是返回1
                flag = true;
            }
        }
        DBconn.closeConn();
        return flag;       //不存在是返回0
    }

    /**
     * 检查用户密码是否一致
     * @param user
     * @return
     * @throws SQLException
     */
    @Override
    public boolean checkPasswdInfo(User user) throws SQLException {
        boolean flag = false;
        DBconn.init();
        //查询是否存在该用户名
        ResultSet rs = DBconn.selectSql("select user_pw from voicerobot.user_info where user_name='" + user.getName() + "';");
        while (rs.next()) {
            if (rs.getString("user_pw").equals(user.getPwd())) {
                //一致是返回true
                flag = true;
            }
        }
        DBconn.closeConn();
        return flag;       //不存在是返回false
    }

    /**
     * 获取登录用户的除密码外的数据。
     *
     * @return
     * @throws SQLException
     */
    @Override
    public User getUserAll(User user) throws SQLException {
        DBconn.init();

        ResultSet rs = DBconn.selectSql("select * from voicerobot.user_info where user_name='" + user.getName() + "';");

        while (rs.next()){
            // rs.next();
            int id = rs.getInt("id");
            // int id = rs.getInt(1);
            String username = rs.getString("user_name");
            String data = rs.getString("data");

            // System.out.println(" aaa " + username + data);

            user.setId(id);
            user.setName(username);
            user.setData(data);
        }


        DBconn.closeConn();
        return user;
    }

    /**
     * 获取数据库中所有用户除密码外的所有数据
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List getAllUser() throws SQLException {
        DBconn.init();
        User user = new User();
        List list = new ArrayList();
        ResultSet rs = DBconn.selectSql("select id, user_name, data from voicerobot.user_info ;");
        while (rs.next()) {
            int id = rs.getInt("id");
            String username = rs.getString("user_name");
            String data = rs.getString("data");

            user.setId(id);
            user.setName(username);
            user.setData(data);
            list.add(user);
            DBconn.closeConn();
        }
        return list;
    }


    /*
     *注册:将用户数据（用户名、密码）存入到数据库中
     */
    public boolean save(User user) throws SQLException {
        boolean flag = false;
        DBconn.init();
        // String sql = "insert into voicerobot.user_info (user_name, user_pw,data) VALUES ('" + user.getName() + "'," + user.getPwd() +"," +user.getData() + ");";
        // System.out.println(sql);
        int i = DBconn.exec("insert into voicerobot.user_info (user_name, user_pw,data) VALUES ('" + user.getName() + "'," + user.getPwd() + ",' " + user.getData() + "');");
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }


}  